﻿namespace IBTrader.Modules.Log
{
    using AxTWSLib;
    using IBTrader.Modules.Tws;
    using NLog;
    using System;
    using System.Windows.Forms;

    class ApplicationInfo
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public event EventHandler<UpdateArgs> Updated;
//        internal static string Txt;
//        internal static bool Demo = false;
//        internal static bool Simulation = false;
        private readonly IIB ib;
        public ApplicationInfo(IIB ib)
        {
            this.ib = ib;
            logger.Debug("Created");
        }
        public void Update(object _, EventArgs __)
        {
            ib.updateAccountValue += UpdateAccountValues;
            ib.updateAccountTime += UpdateAccountTimes;
            ib.reqAccountUpdates(1, ib.AccountName);
            logger.Debug("Update");
        }
        private void UpdateAccountValues(object sender, _DTwsEvents_updateAccountValueEvent e)
        {
            ib.updateAccountValue -= UpdateAccountValues;
            var Txt = string.Format("{0}[{1}]", Text(e.accountName), ib.ClientId);
            if (Updated != null) Updated(this, new UpdateArgs { Text = string.Format("Version {0} {1}", Application.ProductVersion, Txt) });
            logger.Debug("UpdateAccountValues");
        }
        private void UpdateAccountTimes(object sender, _DTwsEvents_updateAccountTimeEvent e)
        {
            ib.updateAccountTime -= UpdateAccountTimes;
            logger.Debug("UpdateAccountTimes");
        }
        private static string Text(string accountName)
        {
            switch (accountName)
            {
                case "U546772":
                case "U1065238": return "Live";
                case "DU135138":
                case "DU54972":
                    {
                        //Demo = true;
                        return "Demo";
                    }
                default:
                    {
                        //Simulation = true;
                        return "Simul";
                    }
            }
        }
        public class UpdateArgs : EventArgs
        {
            public string Text { get; set; }
        }
    }
}